<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="股票代码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入股票代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="股票名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入股票名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="开盘价" prop="openPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.openPrice"-->
<!--          placeholder="请输入开盘价"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="昨日收盘价" prop="lastClosePrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.lastClosePrice"-->
<!--          placeholder="请输入昨日收盘价"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最高价" prop="maxPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.maxPrice"-->
<!--          placeholder="请输入最高价"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最低价" prop="minPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.minPrice"-->
<!--          placeholder="请输入最低价"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最新价格" prop="latestPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.latestPrice"-->
<!--          placeholder="请输入最新价格"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['operate:stock:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockList">
      <el-table-column label="股票代码" align="center" prop="code" />
      <el-table-column align="center" label="发行人" prop="issuer" width="150">
        <template slot-scope="scope">
          <a href="#" style="color: #1c84c6" @click="handleQueryResume(scope.row.owner)"><span>{{
              scope.row.issuer
            }}</span></a>
        </template>
      </el-table-column>
      <el-table-column label="股票名称" align="center" prop="name" />
      <el-table-column label="开盘价(￥)" align="center" prop="openPrice" />
      <!--      <el-table-column label="昨日收盘价(￥)" align="center" prop="lastClosePrice" />-->
      <!--      <el-table-column label="最高价(￥)" align="center" prop="maxPrice" />-->
      <!--      <el-table-column label="最低价(￥)" align="center" prop="minPrice" />-->
      <el-table-column label="最新价格(￥)" align="center" prop="latestPrice" />
      <!--      <el-table-column label="总成交股数" align="center" prop="volume" />-->
      <!--      <el-table-column label="总成交金额(￥)" align="center" prop="turnover" />-->
      <el-table-column label="股票数量" align="center" prop="amount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['operate:stock:edit']"
          >申购</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人才股票对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body style="margin: 15vh" width="800px">
      <el-descriptions :column="2" border style="margin-bottom: 50px">
        <el-descriptions-item label="股票代码">{{ stock.code }}</el-descriptions-item>
        <el-descriptions-item label="股票名称">{{ stock.name }}</el-descriptions-item>
        <el-descriptions-item label="开盘价（￥）">{{ stock.openPrice }}</el-descriptions-item>
        <el-descriptions-item label="最新价格（￥）">
          <el-tag>{{ stock.latestPrice }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="股票数量">{{ stock.amount }}</el-descriptions-item>
      </el-descriptions>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="申购价格（￥）" prop="price">
          <el-input v-model="form.price" placeholder="请输入申购价格" readonly type="number"/>
        </el-form-item>
        <el-form-item label="申购数量" label-width="120px" prop="number">
          <el-input v-model="form.number" placeholder="请输入申购数量" type="number"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <ResumeDetail ref="editDialog"></ResumeDetail>
  </div>
</template>

<script>
import {getStock, listStock} from "@/api/operate/stock";
import {addStockApply} from "@/api/invest/stockApply";
import ResumeDetail from "@/views/publish/resume/detail"

export default {
  name: "Stock",
  components: {ResumeDetail},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人才股票表格数据
      stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        openPrice: null,
        lastClosePrice: null,
        maxPrice: null,
        minPrice: null,
        latestPrice: null,
        volume: null,
        owner: null,
      },

      //申购弹窗
      stock: "",
      // 表单参数
      form: {
        stockId: "",
        number: "",
        price: "",
        action: "0"  //买入操作
      },
      // 表单校验
      rules: {
        number: [
          { required: true, message: '请输入申购数量', trigger: 'blur' },
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人才股票列表 */
    getList() {
      this.loading = true;
      listStock(this.queryParams).then(response => {
        this.stockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stockId: "",
        number: "",
        price: "",
        action: "0"  //买入操作
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 申购操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStock(id).then(response => {
        this.stock = response.data;
        this.form.stockId = response.data.id
        this.form.price = response.data.latestPrice
        this.open = true;
        this.title = "发起申购委托";
      });
    },

    // 查看简历
    handleQueryResume(userId) {
      console.log(userId)
      this.$refs['editDialog'].open = true;
      this.$refs['editDialog'].init(userId);
      this.$refs['editDialog'].getStory(userId);
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addStockApply(this.form).then(response => {
            this.msgSuccess("新增成功");
            this.open = false;
            this.getList();
            this.$router.push({path: '/invest/stockApply',});
          })
        }
      });
    },

  }
};
</script>
